import java.net.*;
import java.util.Scanner;
import java.io.*;

public class ClientThread extends Thread {
    ChatPanel chat;
    private Socket socket;
    private ChatQueue chatqueue;
    private int id;
    Scanner sc;
    
    public ClientThread(Scanner s, Socket socket, ChatQueue chatqueue, ChatPanel panel, int id) {
    	sc = s;
        chat = panel;
        this.socket = socket;
        this.chatqueue = chatqueue;
        this.id = id;
    }
    
    public void run() {
        try {
            //while (!sc.hasNext()) System.out.println("clientThread 21");
            if(id != 6)
                {
                String username = sc.next(), msg = "";
                    while(sc.hasNext())
                            msg += sc.nextLine();
                    sc.close();
                    if(id == 0) {
                        msg = socket.getRemoteSocketAddress().toString().split(":")[0];
                    }
                    chatqueue.add(id,username,msg);
            }
            else
            {
                    while (!sc.hasNext())System.out.println("clientThread 41");
                        int size = sc.nextInt();
                        for(; size > 0; size--)
                        {
                                String temp = sc.nextLine();
                                if(temp.length() == 0)
                                        size++;
                                else
                                {
                                        chat.addIP(temp);
                                }
                        }
            }
            socket.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } 
    }
}